//
//  GetSkuVm.swift
//  Pods
//
//  Created by eme on 2017/3/30.
//
//

import RxSwift
import SwiftyJSON
import NetWorkCore
import ModelProtocol
import RickonCore
import AddCarCore

class GetSkuVm{
    
    let getSKU = PublishSubject<String>()
    
    let backSkuDate = PublishSubject<ProductModel>()
    
    let error = PublishSubject<MikerError>()
    
    let disposeBag = DisposeBag()
    
    let addcartsSuccess = PublishSubject<JSON>()
    
    let addcartsSubject = PublishSubject<NSString>()
    
    init(){
        
        let skuRequest = getSKU.map{ pdid in
            return MineApi.glproductskus(pdid: pdid)
            }.shareReplay(1)
        
        let skuResponse = skuRequest.emeRequestApiForObj(ProductModel.self).shareReplay(1)
        
        
        skuResponse.subscribe(onNext:{[unowned self] result in
            switch result{
            case .success(let data):
                self.backSkuDate.onNext(data)
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        addcartsSubject
            .map{ osCarts in return AddCarApi.addcarts(osCarts: osCarts)}
            .emeRequestApiForRegJson()
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.addcartsSuccess.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
    }
    
    
}
